package com.mcy.business.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.mcy.business.error.ExceptionUtil;
import com.mcy.business.error.RRException;
import com.mcy.business.feign.OrderClient;
import com.mcy.business.feign.ProductClient;
import com.mcy.business.feign.UserClient;
import com.mcy.business.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Slf4j
@RestController
@RequestMapping("business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 下单
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @PostMapping("booking")
    public String booking(String userId, String productId, int count, HttpServletRequest request) {
        System.err.println("bussness获取的id:"+request.getHeader("GLOBAL_LOG_PRIFIX"));
        return businessService.booking(userId,productId,count);
    }

    /**
     * 配置限流器
     * @param name
     * @return
     */
    @RequestMapping(value = "/sayHello", method = RequestMethod.GET)
    @ResponseBody
    @SentinelResource(value = "sayHello",fallback = "doFallback",defaultFallback = "defaultFallback")
    public String sayHello(String name) {
        if(StringUtils.isEmpty(name)){
            throw new RRException("用户名称不能为空!", HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        return "Hello, " + name;
    }


    /**
     * Sentinel限流配置:
     *   https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
     *   https://github.com/alibaba/Sentinel/blob/master/sentinel-extension/sentinel-annotation-aspectj/README.md
     *   https://github.com/alibaba/Sentinel/blob/master/sentinel-demo/sentinel-demo-annotation-spring-aop/src/main/java/com/alibaba/csp/sentinel/demo/annotation/aop/service/TestServiceImpl.java
     * speakHello方法简单配置通用阻塞熔断异常
     */
    @RequestMapping(value = "/speakHello", method =RequestMethod.GET)
    @ResponseBody
    @SentinelResource(value = "speakHello",blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
    public String speakHello(String name) {
        if(StringUtils.isEmpty(name)){
            throw new RRException("用户名称不能为空!", HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        return "Hello, " + name;
    }
    /**
     * Sentinel限流配置:
     *   https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
     */
    public String doFallback(String name, Throwable t) {
        // Return fallback value.
        System.err.println("我是指定的熔断器，异常信息是:"+t.getMessage());
        t.printStackTrace();
        return "fallback:"+name;
    }
    /**
     * Sentinel限流配置:
     *   https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
     */
    public String defaultFallback(Throwable t) {
        System.err.println("我是默认的熔断器,异常信息是:"+t.getMessage());
        return "default_fallback";
    }

}
